// pagesProperty/pages/survey/addQuestion/index.js
var app = getApp(), fetchApi = require("../../../../utils/httpPost.js"), common_Js = require("../../../../component/common.js");

import Toast from '../../../../component/vant/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoading: true,
    survey_id:0,
    question_id:0,
    submitState:false,
    question:{
      id:0,
      propertyId:0,
      survey_id:0,
      questionTitle:'',
      question:'',
      answerSection:'',
      questionType:0,
      state:0,
      sort:0,
    },
    titlePlaceholder:'请输入选项的描述内容',
    
    sections: [
      { value: '',check:''}, { value: '',check:''},
      { value: '',check:''}, { value: '',check:''}
    ],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.s_id){
      this.data.survey_id = parseInt(options.s_id);
    }
    if(options.q_id){
      this.data.question_id = parseInt(options.q_id);
      this.onHttpInit();
    }else{
      this.setData({
        isLoading:false
      })
    }
  },

  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/survey/question/detail', {
      survey_id : that.data.survey_id,
      question_id : that.data.question_id,
    }, function (res) {
      if (res.data.code == 0) {
        if(res.data.data.question != undefined){
          var sections = JSON.parse(res.data.data.question.answerSection);
          that.setData({
            sections:sections
          })
        }
        that.data.question.survey_id = res.data.data.survey.id;
        that.data.question.questionTitle = res.data.data.survey.questionTitle;
        that.data.question.propertyId = res.data.data.survey.propertyId;
        console.log('questionTitle',that.data.question.questionTitle)
        that.setData({
          survey: res.data.data.survey,
          question: res.data.data.question,
          // questions: res.data.data.questions,
          title: res.data.data.survey.questionTitle,
          content: res.data.data.survey.question,
        })
        
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  addSection(){
   var section = { value: '',check:''};

   this.data.sections.push(section);
   this.setData({
    sections: this.data.sections
   })
  },
  delSection(e){
    var index = e.currentTarget.dataset.index;
    this.data.sections.splice(index,1);
    
    this.setData({
      sections: this.data.sections
    })
  },
  bindinput(e){
    var key = e.currentTarget.dataset.key;
    if(key == 'title'){
      this.data.question.question = e.detail.value;
    }
    console.log('bindinput e.detail',e)
  },
  inputSection(e){
    var index = e.currentTarget.dataset.index;
    this.data.sections[index].value = e.detail;
    console.log('this.data.sections',this.data.sections);
  },
  submitOp(){
    console.log('this.data.question',this.data.question)
    if(this.data.question.question  == ''){
      Toast("请输入描述内容");
      return;
    }
    
    for(var index in this.data.sections){
      if(this.data.sections[index].value == ''){
        Toast("请输入选项内容");
        break;
      }
    }

    var sections = JSON.stringify(this.data.sections);
    console.log( 'sections',sections )
    this.data.question.answerSection = sections;

    this.onHttpSubmit();
  },
  
  /**
   * 获取页面数据的 http请求
   */
  onHttpSubmit: function () {
    var that = this;
    fetchApi.httpPost('/survey/add/question', {
      survey_id : that.data.survey_id,
      question : that.data.question,
    }, function (res) {
      if (res.data.code == 0) {
        Toast.success(res.data.msg);
        setTimeout(function(){
          wx.navigateBack({
            delta: -1,
          })
        },500)
      }else{
        Toast.fail(res.data.msg);
      }
    }, function (res) {
      
    })
  },
})